﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using MapEngine;

namespace Dark_Dungeon
{
    [Serializable()]
    public class Player : Character
    {

        PlayerState playerState = PlayerState.Stopped;

        // Delegados
        //public delegate void Dead();

        #region -- Constructor --
        public Player(Vector2 Position)
            : base(Position)
        {
            this.graphics.texture = EntityTexture.Character;
            this.graphics.tile = Point.Zero;

            this.Name = "Jugador";
            this.baseAttack = 6;
        }
        #endregion



        #region -- Serialización/Deserialización --
        public Player(SerializationInfo info, StreamingContext ctxt)
            : base(info, ctxt)
        {
            playerState = (PlayerState)info.GetValue("playerState", typeof(PlayerState));
        }
        
        public override void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("playerState", playerState);
            base.GetObjectData(info, ctxt);
        }
        #endregion

        #region -- Update --
        public void Update(GameTime gameTime, TileMap map, ref Turno turn)
        {
            if (this.HitPoints <= 0)
            {
                Alive = false;
                Debug.AddStaticText("Estás muerto.");
            }

            if (this.Alive)
            {

                Controls.newState = Keyboard.GetState();

                switch (playerState)
                {
                    #region -- Movimiento --
                    case PlayerState.Stopped:

                        direction = EntityDirection.NoDirection;
                        attackDirection = EntityDirection.NoDirection;

                        if (Controls.checkKey(Controls.MOVE_UP))
                            direction = EntityDirection.Up;
                        else if (Controls.checkKey(Controls.MOVE_DOWN))
                            direction = EntityDirection.Down;
                        else if (Controls.checkKey(Controls.MOVE_LEFT))
                            direction = EntityDirection.Left;
                        else if (Controls.checkKey(Controls.MOVE_RIGHT))
                            direction = EntityDirection.Right;
                        else if (Controls.checkKey(Controls.ATTACK))
                        {
                            // Reemplazar por mensaje en pantalla o en registro de eventos
                            Debug.AddStaticText("¿A donde atacar?");
                            playerState = PlayerState.Attacking;
                        }

                        if (direction != EntityDirection.NoDirection)
                        {
                            Move(map, direction);

                            map.setOccupied(position, true);

                            turn = Turno.IA;
                            playerState = PlayerState.Moving;
                        }

                        break;

                    #endregion

                    case PlayerState.Moving:
                        playerState = PlayerState.Stopped;
                        break;

                    case PlayerState.Attacking:


                        attackDirection = EntityDirection.NoDirection;

                        if (Controls.checkKey(Controls.MOVE_UP))
                            attackDirection = EntityDirection.Up;
                        else if (Controls.checkKey(Controls.MOVE_DOWN))
                            attackDirection = EntityDirection.Down;
                        else if (Controls.checkKey(Controls.MOVE_LEFT))
                            attackDirection = EntityDirection.Left;
                        else if (Controls.checkKey(Controls.MOVE_RIGHT))
                            attackDirection = EntityDirection.Right;

                        if (attackDirection != EntityDirection.NoDirection)
                            playerState = PlayerState.Stopped;

                        break;
                }


                Controls.oldState = Controls.newState;


            }
            else
            {
                // Jugador Muerto

            }
        }
        #endregion
    }

    enum PlayerState
    {
        Stopped, Moving, Attacking
    }
}
